﻿using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using AceBots.Xbee;
using AceBots.ARPacket;


using GHIElectronics.NETMF.FEZ;

namespace XbeeAPITest
{
    public class Program
    {
       
        static bool gbLEDState;
        static OutputPort led;
        private static XbeeV1Module myXbee;
        // Some constants
        private const int ciLedOn=1;
        private const int ciLedOff=2;
        private const int ciSendData = 3;

        public static void ProcessPacket(XbeeV1FrameMgr xbFrame)
        {
            // This function is called if we got a complete APIFrame from the Xbee Radio
            // It was associated with the Xbee module in main and is a callback function.
            // Note This function gets called with a TXStatus packet after every transmission
            // of an APIPacket, however that TXStatus packet is not currently handled. 
            // ARPacket class will probably be extended in a future release to handle this.
            //
            AceBots.ARPacket.ARPacket objARPacket = new AceBots.ARPacket.ARPacket(xbFrame);
            if (objARPacket.PacketType == ARPacketTypes.ShortCommand ||
                objARPacket.PacketType == ARPacketTypes.VerboseCommand)
            {
                if (objARPacket.Command == ciLedOn) gbLEDState = true; // Turn on the LEd
                if (objARPacket.Command == ciLedOff) gbLEDState = false; // Turn off the LED
                if (objARPacket.Command == ciSendData) SendSomeData(); // Send some data out the Xbee
             }
            else // We did not get a command packet so check for data packet types
            {
                // Following is just a stub to handle the data packet types 
                // Does nothing but could be modified to process data packets that are received
               switch (objARPacket.PacketType)
	            {
		            case ARPacketTypes.BinaryReadings:
                        break;
                    case ARPacketTypes.BinaryShortReadings:
                        break;
                    case ARPacketTypes.BinarySingleReadType:
                        break;
                    default:
                        break;
	            }
            }
            
        }

        public static void SendSomeData()
        {
            // Create a frame manager
            XbeeV1FrameMgr XbeeFrameMgr = new XbeeV1FrameMgr(100);
            //Identify the reading (this is typically associated with a sensor)
            ushort readId = 1;
            // Create an ARPacket
            ARPacket objPacket = new ARPacket(XbeeFrameMgr, myXbee, ARPacketTypes.BinaryReadings);
            // Append a pretend reading of type short for readID and increment the readID
            objPacket.AppendData(readId++, (short)100);
            // Append a pretend reading of type byte for readID and increment the readID
            objPacket.AppendData(readId++, (byte)7);
            // Append a pretend reading of type int 32 for readID and increment the readID
            objPacket.AppendData(readId++, (Int32)1048576);
            // Append a pretend reading of type string for readID and increment the readID
            objPacket.AppendData(readId++, "Data");
            // Set the send address to the other xbee module connected to the PC
            XbeeFrameMgr.SendAddress = 4368;
            // Calculate the checksum and length
            objPacket.CalcCheckSumAndLength();
            // Now just send the packet out the radio
            myXbee.SendAPIPacket(XbeeFrameMgr);

        }

        public static void Main()
        {
            // Initialize the Xbee
            myXbee = new XbeeV1Module("COM4", 19200);
            myXbee.FrameCallback = ProcessPacket;
  
            gbLEDState = true; //Inital state for led = on

            led = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di13,gbLEDState);

            while (true)
            {
                // Sleep for 500 milliseconds
                Thread.Sleep(500);
 
                led.Write(gbLEDState);
            }
        }

    }
}
